const app = getApp()
let qCloud = require("../../index.js");

Page({
    /** 页面的初始数据 */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        info: {
            warehouseInNumber: 0,
            warehouseInPrice: 0,
            warehouseOutNumber: 0,
            warehouseOutPrice: 0,
            profitPrice: 0,
        },
        list: []
    },

    // 仓库入库
    toWarehouseIn(e) {
        wx.navigateTo({
            url: "/pages/warehouse/warehousein",
        });
    },

    // 仓库出库
    toWarehouseOut(e) {
        wx.navigateTo({
            url: "/pages/warehouse/warehouseout",
        });
    },

    // 货物入库
    goodsIn(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let goodsInfo = self.data.list[index];
        wx.navigateTo({
            url: "/pages/warehouse/goodsin?goodsId=" + goodsInfo.goodsId,
        });
    },

    // 货物出库
    goodsOut(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let goodsInfo = self.data.list[index];
        wx.navigateTo({
            url: "/pages/warehouse/goodsout?goodsId=" + goodsInfo.goodsId,
        });
    },

    // 货物调仓
    goodsAllocate(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let goodsInfo = self.data.list[index];
        wx.navigateTo({
            url: "/pages/warehouse/goodsallocate?goodsId=" + goodsInfo.goodsId,
        });
    },

    // 开始触摸,重置所有左滑内容
    touchstart: function (e) {
        let self = this;
        self.data.list.forEach(function (item, idx) {
            // 只操作为true的
            if (item.isTouchMove) {
                item.isTouchMove = false;
            }
        });
        self.setData({
            startX: e.changedTouches[0].clientX,
            startY: e.changedTouches[0].clientY,
            list: self.data.list
        });
    },

    // 滑动事件
    touchEvent(e) {
        let self = this; // 防止this指向问题
        let index = e.currentTarget.dataset.index; // 当前下标
        let startX = self.data.startX; // 开始X坐标
        let startY = self.data.startY; // 开始Y坐标
        let touchMoveX = e.changedTouches[0].clientX; // 滑动变化x坐标
        let touchMoveY = e.changedTouches[0].clientY; // 滑动变化y坐标
        // 获取滑动角度
        let angle = self.angle({
            X: startX,
            Y: startY
        }, {
            X: touchMoveX,
            Y: touchMoveY
        });

        self.data.list.forEach(function (item, idx) {
            item.isTouchMove = false;
            // 滑动超过30度，函数返回指定数字 x 的绝对值
            if (Math.abs(angle) > 30) return;
            if (idx == index) {
                if (touchMoveX > startX) // 右滑
                    item.isTouchMove = false;
                else // 左滑
                    item.isTouchMove = true;
            }
        });

        // 更新数据
        self.setData({
            list: self.data.list
        })
    },

    bindGoTo: function (e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        })
    },

    // 计算滑动角度，start 起点坐标，end 终点坐标
    angle: function (start, end) {
        let _X = end.X - start.X;
        let _Y = end.Y - start.Y;
        // 返回角度 Math.atan()函数返回一个数值的反正切（以弧度为单位）
        return 180 * Math.atan(_Y / _X) / (2 * Math.PI);
    },

    /** 生命周期函数--监听页面加载 */
    onLoad: function (options) {
        let self = this;
        // 查询仓储统计
        self.queryWarehouseStatistic();
        // 查询仓储货物列表
        self.queryGoodsList();
    },

    // 查询仓储统计
    queryWarehouseStatistic: function () {
        let self = this;
        let params = {
            enterpriseId: wx.getStorageSync("loginInfo").organId
        }

        app.fetchApis(self, qCloud.api.warehouse_queryWarehouseStatistic, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                self.setData({
                    info: result.data,
                })
            }
        });
    },

    // 查询仓储货物列表
    queryGoodsList: function () {
        let self = this;
        let params = {};
        app.fetchApis(self, qCloud.api.warehouse_queryGoodsList, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                let data = result.data;
                let goodsList = [];
                for (let i = 0; i < data.goodsList.length; i++) {
                    let goodsInfo = data.goodsList[i];
                    let goods = {
                        goodsId: goodsInfo.goodsId,
                        goodsName: goodsInfo.goodsName,
                        goodsTotalNumber: goodsInfo.goodsTotalNumber,
                        warehouseName: goodsInfo.warehouseName,
                        isTouchMove: false
                    };
                    goodsList.push(goods);
                }
                self.setData({
                    list: goodsList,
                });
            }
        });
    },

    /** 生命周期函数--监听页面显示 */
    onShow: function () {
        let self = this;

        // 查询仓储统计
        self.queryWarehouseStatistic();
        // 查询仓储货物列表
        self.queryGoodsList();
    },

    /** 生命周期函数--监听页面卸载 */
    onUnload: function () {
        wx.switchTab({
            url: "/pages/index/index",
        })
    },

})
